package com.atguigu.gmall.pms.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.pms.entity.AttrEntity;
import com.atguigu.gmall.pms.entity.SkuEntity;
import com.atguigu.gmall.pms.mapper.AttrMapper;
import com.atguigu.gmall.pms.mapper.SkuMapper;
import com.atguigu.gmall.pms.vo.SaleAttrValueVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.SkuAttrValueMapper;
import com.atguigu.gmall.pms.entity.SkuAttrValueEntity;
import com.atguigu.gmall.pms.service.SkuAttrValueService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;


@Service("skuAttrValueService")
public class SkuAttrValueServiceImpl extends ServiceImpl<SkuAttrValueMapper, SkuAttrValueEntity> implements SkuAttrValueService {

    @Resource
    private AttrMapper attrMapper;
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private SkuAttrValueMapper skuAttrValueMapper;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<SkuAttrValueEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<SkuAttrValueEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public List<SkuAttrValueEntity> querySearchAttrValuesByCidAndSkuId(Long cid, Long skuId) {
        //根据category的id和searchType获取attr信息集合
        List<AttrEntity> attrEntities = attrMapper.selectList(new LambdaQueryWrapper<AttrEntity>()
                .eq(AttrEntity::getCategoryId, cid).eq(AttrEntity::getSearchType, 1));
        //判断非空
        if (CollectionUtils.isEmpty(attrEntities)){
            return null;
        }
        //如果非空，则再讲attr集合转换成attrid的集合，就是只有attrId的属性
        List<Long> attrIds = attrEntities.stream().map(AttrEntity::getId).collect(Collectors.toList());
        //再根据attrId和skuId查询到SkuAttrValue的信息集合
        List<SkuAttrValueEntity> list = this.list(
                new LambdaQueryWrapper<SkuAttrValueEntity>().eq(SkuAttrValueEntity::getSkuId, skuId)
                .in(SkuAttrValueEntity::getAttrId, attrIds));
        return list;//返回
    }

    @Override
    public List<SaleAttrValueVo> querySkuAttrValueBySpuId(Long spuId) {
        //根据spuId查询sku
        List<SkuEntity> skuEntityList =
                skuMapper.selectList(new LambdaQueryWrapper<SkuEntity>().eq(SkuEntity::getSpuId, spuId));
        if (CollectionUtils.isEmpty(skuEntityList)){
            return null;
        }
        List<Long> skuIds = skuEntityList.stream().map(SkuEntity::getSpuId).collect(Collectors.toList());

        //根据skuId查询skuAttrValue
        List<SkuAttrValueEntity> skuAttrValueEntityList =
                this.list(new LambdaQueryWrapper<SkuAttrValueEntity>().in(SkuAttrValueEntity::getSkuId, skuIds));
        if (CollectionUtils.isEmpty(skuAttrValueEntityList)){
            return null;
        }

        //把skuAttrValue转换成List<SaleAttrValueVo>
        //根据attrId对数据进行分组，以attrId作为key，以分组的数据作为value
        Map<Long, List<SkuAttrValueEntity>> map =
                skuAttrValueEntityList.stream().collect(Collectors.groupingBy(SkuAttrValueEntity::getAttrId));

        List<SaleAttrValueVo> saleAttrValueVos = new ArrayList<>();
        //把每一个kv转换成一个saleAttrValueVo对象
        map.forEach((attrId,attrValueEntities)->{
            SaleAttrValueVo saleAttrValueVo = new SaleAttrValueVo();
            //当前的key就是attrId
            saleAttrValueVo.setAttrId(attrId);
            //有该分组的情况下，该组数据不可能为null，至少有一个元素，从第一个元素中获取attrName
            saleAttrValueVo.setAttrName(attrValueEntities.get(0).getAttrName());
            //规格参数值，去重
            saleAttrValueVo.setAttrValues(attrValueEntities.stream().map(SkuAttrValueEntity::getAttrValue).collect(Collectors.toSet()));
            saleAttrValueVos.add(saleAttrValueVo);
        });
        return saleAttrValueVos;
    }

    @Override
    public List<SkuAttrValueEntity> querySkuAttrValueBySKuId(Long skuId) {
        List<SkuAttrValueEntity> list = this.list(new LambdaQueryWrapper<SkuAttrValueEntity>().eq(SkuAttrValueEntity::getSkuId, skuId));
        return list;
    }

    @Override
    public String queryMappingBySpuId(Long spuId) {
        //根据spuId查询sku
        List<SkuEntity> skuEntityList =
                skuMapper.selectList(new LambdaQueryWrapper<SkuEntity>().eq(SkuEntity::getSpuId, spuId));
        if (CollectionUtils.isEmpty(skuEntityList)){
            return null;
        }
        List<Long> skuIds = skuEntityList.stream().map(SkuEntity::getId).collect(Collectors.toList());
        //查看映射关系
        List<Map<String,Object>> maps = skuAttrValueMapper.queryAttrValueBySkuIds(skuIds);
        if (CollectionUtils.isEmpty(maps)){
            return null;
        }
        //将list转换成map集合
        //把每一个Map<String,Object>转换成kv
        Map<Object, Object> mappingMap =
                maps.stream().collect(Collectors.toMap(map -> map.get("attr_values"), map -> map.get("sku_id")));
        return JSON.toJSONString(mappingMap);
    }

}